




//
//  NSString+PostDate.m
//  PostDelivery
//
//  Created by wmz on 2020/6/30.
//  Copyright © 2020 wmz. All rights reserved.
//

#import "NSString+PostDate.h"

@implementation NSString (PostDate)
+ (NSString*)getFormate:(NSString*)formate date:(NSDate*)date{
    NSDateFormatter *dateFormat = [NSDateFormatter new];
    [dateFormat setDateFormat:formate];
    NSString* string=[dateFormat stringFromDate:date];
    return string;
}
//日期字符串转为时间戳 年月日时分秒
+ (NSTimeInterval)getFormate:(NSString*)dateString{
    NSDateFormatter *dateFormat = [NSDateFormatter new];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date= [dateFormat dateFromString:dateString];
    return [date timeIntervalSince1970];
}
//日期字符串转为日期 年月日时分秒
+ (NSDate*)getFormateDate:(NSString*)dateString{
    NSDateFormatter *dateFormat = [NSDateFormatter new];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date= [dateFormat dateFromString:dateString];
    return date;
}
+ (BOOL)isSameDay:(long)iTime1 Time2:(long)iTime2
{
    //传入时间毫秒数
    NSDate *pDate1 = [NSDate dateWithTimeIntervalSince1970:iTime1];
    NSDate *pDate2 = [NSDate dateWithTimeIntervalSince1970:iTime2];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:pDate1];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:pDate2];
    
    return [comp1 day]   == [comp2 day] &&
    [comp1 month] == [comp2 month] &&
    [comp1 year]  == [comp2 year];
}

//判断两个时间戳相差几天
+ (NSInteger)day:(long)iTime1 Time2:(long)iTime2{
    NSDate *pDate1 = [NSDate dateWithTimeIntervalSince1970:iTime1];
    NSDate *pDate2 = [NSDate dateWithTimeIntervalSince1970:iTime2];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *pDateStr1 = [dateFormatter stringFromDate:pDate1];
    NSString *pDateStr2 = [dateFormatter stringFromDate:pDate2];
    pDate1 = [dateFormatter dateFromString:pDateStr1];
    pDate2 = [dateFormatter dateFromString:pDateStr2];

    
    NSCalendar* calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSDateComponents *cmps = [calendar components:unitFlags fromDate:pDate1 toDate:pDate2 options:NSCalendarWrapComponents];
    NSInteger day = [cmps day];
    NSInteger year = [cmps year];
    NSInteger month = [cmps month];
    if (month>0) {
        day += month*30;
    }
    if (year) {
        day += 365*year;
    }
    return day;
}
//判断过了多久
+ (BOOL)currentTime:(double)time key:(NSString*)key{
    BOOL success = false;
    if (![[NSUserDefaults standardUserDefaults] objectForKey:key]) {
         NSDate *date = [NSDate date];
         NSTimeInterval currentTime= [date timeIntervalSince1970];
         [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%lf",currentTime] forKey:key];
    }else{
        NSDate *date = [NSDate date];
        NSTimeInterval currentTime= [date timeIntervalSince1970];
        double oldTime = [[[NSUserDefaults standardUserDefaults] objectForKey:key] doubleValue];
        if (currentTime>oldTime+time) {
            //到达时间
            success = YES;
        }
    }
    return success;
}

+ (NSString*)maxDate:(NSDate*)maxDate minDate:(NSDate*)minDate{
  NSTimeInterval start = [maxDate timeIntervalSince1970]*1;
  NSTimeInterval end = [minDate timeIntervalSince1970]*1;
  NSTimeInterval value = start - end;
  if (value <= 0) return @" ";
  NSString *days = [NSString stringWithFormat:@"%d",(int)value / (24*3600)];
  NSString *hours = [NSString stringWithFormat:@"%d",(int)(value-days.integerValue*24*3600)/3600];
  NSString *minutes = [NSString stringWithFormat:@"%d",(int)value /60%60];
  NSString *returnStr = @"";
  if (days.intValue) {
      returnStr = [NSString stringWithFormat:@"%@天",days];
  }
    
  return [returnStr stringByAppendingFormat:@"%@时%@分",hours,minutes];
}

@end
